package com.lagou.service.impl;

import com.lagou.dao.TokenMapper;
import com.lagou.pojo.Token;
import com.lagou.service.TokenService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;

import java.util.Optional;
import java.util.UUID;

@Service
public class TokenServiceImpl implements TokenService {

    @Autowired
    private TokenMapper tokenMapper;

    @Override
    public Token getByToken(String token) {
        Token tokenBo = new Token();
        tokenBo.setToken(token);
        Example<Token> example = Example.of(tokenBo);
        Optional<Token> optional = tokenMapper.findOne(example);
        return optional.get();
    }

    @Override
    public Token save(Token token) {
        return tokenMapper.save(token);
    }

    @Override
    public Token createToken(String email) {
        String token = UUID.randomUUID().toString();
        Token bo = new Token();
        bo.setEmail(email);
        bo.setToken(token);
        bo = save(bo);
        return bo;
    }
}
